package craft;

public class Timer
{
	private static final long NS_PER_SECOND = 1000000000L;
	private static final long MAX_NS_PER_UPDATE = 1000000000L;
	private static final int MAX_TICKS_PER_UPDATE = 100;
	private float ticksPerSecond;
	private long lastTime;
	public int ticks;
	public float a;
	public float timeScale = 1.0F;
	public float fps = 0.0F;
	public float passedTime = 0.0F;

	public Timer(int ticksPerSecond) {
		//MAX_TICKS_PER_UPDATE = ticksPerSecond;
		this.ticksPerSecond = ticksPerSecond;
		this.lastTime = System.nanoTime();
	}

	public void advanceTime() {
		long now = System.nanoTime();
		long passedNs = now - this.lastTime;
		this.lastTime = now;

		if (passedNs < 1L) passedNs = 1L;
		if (passedNs > MAX_NS_PER_UPDATE) passedNs = MAX_NS_PER_UPDATE;

		this.fps = (float)(NS_PER_SECOND / passedNs);

		this.passedTime += (float)passedNs * this.timeScale * this.ticksPerSecond / NS_PER_SECOND;

		this.ticks = (int)this.passedTime;
		if (this.ticks > MAX_TICKS_PER_UPDATE) this.ticks = MAX_TICKS_PER_UPDATE;
		this.passedTime -= this.ticks;
		this.a = this.passedTime;
	}
}
